﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript" src="../../../Content/highlighter/scripts/shCore.js"></script>
    <script type="text/javascript" src="../../../Content/highlighter/scripts/shBrushJScript.js"></script>
    <script type="text/javascript" src="../../../Content/highlighter/scripts/shBrushCSharp.js"></script>
    <script type="text/javascript" src="../../../Content/highlighter/scripts/shBrushXml.js"></script>
    <link type="text/css" rel="stylesheet" href="../../../Content/highlighter/styles/shCore.css" />
    <link type="text/css" rel="stylesheet" href="../../../Content/highlighter/styles/shCoreEclipse.css" />
    <script type="text/javascript">SyntaxHighlighter.all();</script>
</head>
<body>
    <h3>调用模板</h3>
    <p>任何cshtml文件都会生成.tempalte.cs文件,其内容如下：</p>
    <pre class="brush:csharp;">
    using System;
    using System.IO;
    using System.Net;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using NFinal;

    //此代码由NFinalCompiler生成。
    //http://bbs.nfinal.com
    namespace NFinalWeb3.Views.Index
    {
        /// &lt;summary>
        /// 模板类
        /// &lt;/summary>
        [View("/NFinalWeb3/Views/Index/Index1.cshtml")]
        public class Index1 : NFinal.View.RazorView&lt;NFinalWeb3.Controllers.Index1Controller_Model.Index>
        {
            /// &lt;summary>
            /// 模板类初始化函数
            /// &lt;/summary>
            /// &lt;param name="writer">写对象&lt;/param>
            /// &lt;param name="Model">数据&lt;/param>
            public Index1(NFinal.IO.Writer writer, NFinalWeb3.Controllers.Index1Controller_Model.Index Model) : base(writer, Model)
            {
            }
            /// &lt;summary>
            /// 输出模板内容
            /// &lt;/summary>
            public override void Execute()
            {
                this.ExecuteAsync().Wait();
            }
            /// &lt;summary>
            /// 输出模板内容(异步)
            /// &lt;/summary>
            public override async Task ExecuteAsync()
            {
                await writer.WriteAsync("");
                await writer.WriteAsync("&lt;!DOCTYPE html>\r\n&lt;html>\r\n&lt;head>\r\n    &lt;meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\r\n    &lt;title>&lt;/title>\r\n\t&lt;meta http-equiv=\"Cache-Control\" content=\"no-cache\">\r\n&lt;/head>\r\n&lt;body>\r\n    ");
                await writer.WriteAsync(Model);
                await writer.WriteAsync("\r\n&lt;/body>\r\n&lt;/html>");
            }
        }
    }
    </pre>
    <p>其中View特性中[View("/NFinalWeb3/Views/Index/Index1.cshtml")]的参数为该模板路径
    </p>
    <pre class="brush:csharp;">
        public Index1Controller : BaseController
        {
            public ActionResult Index()
            {
                //声明视图数据
                var viewModel=new NFinalWeb3.Controllers.Index1Controller_Model.Index();
                //NFinal.IO.Writer输出类的介绍：
                //调用控制器输出
                var actionWriter=this;//控制器本身就是Writer的子类。
                var actionTemplate=new NFinalWeb3.Views.Index.Index1(actionWriter,viewModel);
                actionTemplate.Execute();//异步 await actionTemplate.ExecuteAsync();
                //调用文件输出，生成静态文件
                var fileWriter=new NFinal.IO.FileWriter("C:\index.html");
                var fileTemplate=new NFinalWeb3.Views.Index.Index1(fileWriter,viewModel);
                fileTemplate.Execute();//异步 await fileTemplate.ExecuteAsync();
                //调用流输出，在内存中生成
                var streamWriter=new NFinal.IO.StreamWriter(new MemroyStream());
                var streamTemplate=new NFinalWeb3.Views.Index.Index1(streamWriter,viewModel);
                streamTemplate.Execute();//异步 await streamTemplate.ExecuteAsync();
                //调用字符串输出
                var stringWriter=new NFinal.IO.StringWriter();
                var stringTemplate=new NFinalWeb3.Views.Index.Index1(stringWriter,viewModel);
                stringTemplate.Execute();//异步 await stringTemplate.ExecuteAsync();

                //如果在控制器下调用
                return View("/NFinalWeb3/Views/Index/Index1.cshtml",viewModel);
                //如果模板数据类型为该操作模板数据类型，则可简写为
                return View("/NFinalWeb3/Views/Index/Index1.cshtml");
                //如果视图路径与该操作路径对应,则可简写为
                return View();
            }
        }
    </pre>
</body>
</html>